/* add to fav */
function addfav(url,title){var fav_url = url;var fav_title = title;if (document.all && window.external){window.external.AddFavorite(fav_url,fav_title);}else if (window.sidebar){window.sidebar.addPanel(fav_title,fav_url,"");}else{alert(js_fav);}
}
// trim
function trim_str(str){return str.replace(/(^\s*)|(\s*$)/g,"");}
// change language 改变语言
function set_lang(lang,currency){
	$.cookie('lang', lang, '{ expires: 14,secure: true}'); top.location.reload();
	set_currency(currency);
}
function set_currency(currency){
	$.cookie('currency', currency, '{ expires: 14,secure: true}'); top.location.reload();
}
/* confirm delete */
function delc(){ return confirm(js_comfirm_delete);}
function operator(){ return confirm(js_comfirm_delete);}
function checkf(id){
	if($(id).val()=='') return false; return true;
}
/* product options */
function option_change(field){
	$('#no_'+field).addClass('hide');
	var will_change_price=true;
	$.get("ajax_option.php?"+field+"_oid="+$('#variation_'+field).val(),{'pid':$('#buy-pid').val(),'do':'get_related_options'}, function(data){
		//$.warning(data);
		//will_change_price=true;
		json = JSON.parse(data);
		if ($('#variation_color').length){
			if(field!='color'){
				//update color option
				$("#variation_color > option").each(function() {
					if(this.value!='') {
						if(json.color.indexOf(this.value)<0) {
							$("#variation_color option[value="+this.value+"]").attr('disabled','disabled');
						}
						else {
							$("#variation_color option[value="+this.value+"]").removeAttr('disabled');
						}
					}
				});
			}
			/*
			else {
				$("#variation_color > option").each(function() {
					$("#variation_color option[value="+this.value+"]").removeAttr('disabled');
				});
			}
			*/
		}
		if ($('#variation_size').length){
			if(field!='size') {
				//update size option
				$("#variation_size > option").each(function() {
					if(this.value!='') {
						if(json.size.indexOf(this.value)<0) {
							$("#variation_size option[value="+this.value+"]").attr('disabled','disabled');
						}
						else {
							$("#variation_size option[value="+this.value+"]").removeAttr('disabled');
						}
					}
				});
			}
			/*
			else {
				$("#variation_size > option").each(function() {
					$("#variation_size option[value="+this.value+"]").removeAttr('disabled');
				});			
			}
			*/
		}
		if ($('#variation_other1').length) {
			if(field!='other1') {
				//update other1 option
				$("#variation_other1 > option").each(function() {
					if(this.value!='') {
						if(json.other1.indexOf(this.value)<0) {
							$("#variation_other1 option[value="+this.value+"]").attr('disabled','disabled');
						}
						else {
							$("#variation_other1 option[value="+this.value+"]").removeAttr('disabled');
						}
					}
				});
			}
			/*
			else {
				$("#variation_size > option").each(function() {
					$("#variation_size option[value="+this.value+"]").removeAttr('disabled');
				});			
			}
			*/
		}
		if ($('#variation_other2').length) {
			if(field!='other2') {
				//update other2 option
				$("#variation_other2 > option").each(function() {
					if(this.value!='') {
						if(json.other2.indexOf(this.value)<0) {
							$("#variation_other2 option[value="+this.value+"]").attr('disabled','disabled');
						}
						else {
							$("#variation_other2 option[value="+this.value+"]").removeAttr('disabled');
						}
					}
				});
			}
			/*
			else {
				$("#variation_size > option").each(function() {
					$("#variation_size option[value="+this.value+"]").removeAttr('disabled');
				});
			}
			*/
		}
	});
	if(will_change_price){ // 更改显示的价格
		var url=$('.pid').val();
		var price=$('.item_price').html();
		if($('#variation_color').length) url=url+','+$('#variation_color').val();
		else url=url+',0';
		if($('#variation_size').length) url=url+','+$('#variation_size').val();
		else url=url+',0';
		if($('#variation_other1').length) url=url+','+$('#variation_other1').val();
		else url=url+',0';
		if($('#variation_other2').length) url=url+','+$('#variation_other2').val();
		else url=url+',0';
		$.get("ajax_option.php?url="+url,{'do':'get_related_price'}, function(data){
			json = JSON.parse(data);
			$('.item_price').html(json.price);
			$('#buy-var').val(json.varid);
			// 更新组合价格
			cross_buy(0);
		});
		//alert('abc');
	}
}
function cross_buy(go){
	var url='ajax_cart.php?do=cross';
	var cross_price = parseFloat(parseFloat($('.item_price').html()) * parseFloat($('#buy-qty').val())).toFixed(2);
	pids='&pids=' + $('.pid').val();
	vars='&vars=' + $('#buy-var').val();
	qtys='&qtys=' + $('#buy-qty').val();
	$(".cross_more input[type='checkbox']:checked").each(function(){
			pids=pids+','+$(this).attr('data');
			cross_price=cross_price*1+parseFloat($(this).attr('data-price')).toFixed(2)*1;
			vars=vars+',0';
			qtys=qtys+',1';
	});
	url=url+pids+vars+qtys;
	cross_price = parseFloat(cross_price).toFixed(2);
	if(go == '0'){
		$('.span_cross_price').html(cross_price);
	}
	if(go == '1') goto(url);
}
function add_to_cart(){
	var good_to_continue=true;
	var no_need_var=true;
	if ($('#variation_color').length){
		if($('#variation_color').val() == ''){
			$("#no_color").removeClass("hide");
			good_to_continue=false;
			no_need_var=false;
		}
		else {
			$("#no_color").addClass("hide");
			$('#color_oid').val($('#variation_color').val());
		}
	}
	
	if ($('#variation_size').length){
		if($('#variation_size').val() == ''){
			$("#no_size").removeClass("hide");
			good_to_continue=false;
			no_need_var=false;
		}
		else {
			$("#no_size").addClass("hide");
			$('#size_oid').val($('#variation_size').val());
		}
	}
	
	if ($('#variation_other1').length){
		if($('#variation_other1').val() == ''){
			$("#no_other1").removeClass("hide");
			good_to_continue=false;
			no_need_var=false;
		}
		else {
			$('#other1_oid').val($('#variation_other1').val());
		}
	}
	
	if ($('#variation_other2').length) {
		if($('#variation_other2').val() == ''){
			$("#no_other2").removeClass("hide");
			good_to_continue=false;
			no_need_var=false;
		}
		else {
			$('#other2_oid').val($('#variation_other2').val());
		}
	}
	
	if(!good_to_continue) {
		return false;
	}
	
	if(no_need_var){
		$.get("ajax_cart.php",{'pid':$('#buy-pid').val(),'varid':$('#buy-var').val(),'qty':$('#buy-qty').val(),'do':'add'}, function(data){
				//alert(data);
				$('.toolcart').parent().remove();$('.center_box').fadeIn();$('#cart_qty').html(data);
			});
	}
	// varid 已修正, 此处废弃
	// else {
	// 	$.get("ajax_variation.php",{'pid':$('#buy-pid').val(),'color_oid':$('#color_oid').val(),'size_oid':$('#size_oid').val(),'other1_oid':$('#other1_oid').val(),'other2_oid':$('#other2_oid').val(),'do':'get_id'}, function(data){
	// 		if(data==0) {
	// 			$.warning(js_no_such_product);
	// 		}
	// 		else {
	// 			$('#buy-var').val(data);
	// 			$.get("ajax_cart.php",{'pid':$('#buy-pid').val(),'varid':$('#buy-var').val(),'qty':$('#buy-qty').val(),'do':'add'}, function(data){
	// 				//alert(data);
	// 				$('.toolcart').parent().remove();$('.center_box').fadeIn();$('#cart_qty').html(data);
	// 			});			
	// 		}
	// 	});
	// }
}
function add_to_cart_multi(i,refresh){
	$.get("ajax_cart.php",{'pid':$('#buy-pid'+i).val(),'varid':$('#buy-var'+i).val(),'qty':$('#buy-qty'+i).val(),'do':'add'}, function(data){
		//alert(data);
		$('.toolcart').parent().remove();
		$('#cart_qty').html(data);
		$.get("ajax_cart.php",{'do':'get_order_price'}, function(data){
			//alert(data);
			$('#cart_order_price').html(data);
			if(refresh) {
				location.reload();
			}
		});
    });
}
function cart_qty_add(pid,varid,qty,max){
	if(qty==max) { $.notice(js_max_stock); return false; }
	else qty=qty+1;
	$.get("ajax_cart.php",{'pid':pid,'varid':varid,'qty':qty,'do':'update'}, function(data){
		top.location.reload();
    });
}
function cart_qty_reduce(pid,varid,qty){
	if(qty==1) return false;
	else qty=qty-1;
	$.get("ajax_cart.php",{'pid':pid,'varid':varid,'qty':qty,'do':'update'}, function(data){
		top.location.reload();
    });
}
function cart_qty_update(pid,varid,qty,max){
	if(qty>max) qty=max;
	$.get("ajax_cart.php",{'pid':pid,'varid':varid,'qty':qty,'do':'update'}, function(data){
		top.location.reload();
    });
}
function cart_remove(pid,varid){
	$.get("ajax_cart.php",{'pid':pid,'varid':varid,'do':'remove'}, function(data){
		top.location.reload();
    });
}
function cart_clear(){
	$.get("ajax_cart.php",{'do':'clear'}, function(data){
		top.location.reload();
    });
}
function history_clear(){
	$.get("ajax_history.php",{'do':'clear'}, function(data){
		$('.history').fadeOut(); $.notice(js_history_cleared);
    });
}
function order_submit(){
	$('.order_form').submit();
}
function ajax_delete(table,id){
	if(!confirm(js_comfirm_delete)) return false;
	var div = '.delete'+id;$.get("ajax_delete.php","table="+table+"&id="+id, function(msg){
		if(msg=='1'){
		$(div).fadeOut('slow').remove(); $.notice(js_deleted);
	}});
}
function qty_add (qty,maxer){
	var div='#'+qty;
	$(div).val( parseInt($(div).val()) +1);if(parseInt($(div).val())>maxer)$(div).val(maxer);
	// 更新组合价格
	cross_buy(0);

}
function qty_reduce(qty){
	var div='#'+qty;
	$(div).val( parseInt($(div).val()) - 1);if(parseInt($(div).val())<1)$(div).val(1);
	// 更新组合价格
	cross_buy(0);
}
function cart_quantity_check(max_qty){
	var reg = /^\d+$/;
	var str_qty = $('#buy-qty').val();
    if(str_qty.constructor === String){
        var re = str_qty.match(reg);
        if(re == null) $('#buy-qty').val(1);
    }
    if(parseInt(str_qty) >parseInt(max_qty)) $('#buy-qty').val(max_qty);
	// 更新组合价格
	cross_buy(0);
}
function payoff(id){
	var div='.shipfee'+id; var payoff=0;
	$('.shipfee').html($(div).val());
	$('.shipfeeinput').val($(div).val());
	payoff=(parseFloat($(div).val())+parseFloat($('#subtotal').html())).toFixed(2);
	$('#payoff').html(payoff);
	$('#L_AMT0').val(payoff);
}
function goto(url){ window.location = url; }

function search(){
	if(! checkf('.search-text')) { $('.search-text').focus(); return false; }
	return true;
}

// jQuery 扩展
$.extend({
 notice:function (options){
 	//var params=options?options:{'message':'This is a notice','timer':2};
 	var str='<div class="notice"><div>'+options+'</div></div>';
 	$('body').append(str);
 	$('.notice').css('top',($(window).height())/2-($('.notice').height()));
	$('.notice').css('left',($(window).width())/2-($('.notice').width()/2));
 	var timer = 3.8*1000;
	setTimeout("$('.notice').fadeIn()", 380);
	setTimeout("$('.notice').fadeOut('slow').remove()", timer);
	//$('.notice');
 }});
$.extend({
 warning:function (options){
 	var close=js_close;
 	var str='<div class="mask_overlay"><div class="mask_box"><div class="mask_boxer"><div class="mask_message">'+options+'</div><div class="mask_close"><a onclick="$(\'.mask_overlay\').fadeOut().remove();">[ '+close+' ]</a></div></div></div></div>';
 	$('body').append(str);
	$('.mask_overlay').css('height',$('body').height()+200);
	$('.mask_box').css('left',($(window).width())/2-($('.mask_box').width()/2));
	$('.mask_box').css('top',($(window).height())/2-($('.mask_box').height()));
	setTimeout("$('.mask_overlay').fadeIn()", 38);
 }
});
// document ready
$(document).ready(function(){
	$(".minphoto a").click(function(){
		var curr=$(this).attr('data');
		$('.spec_n1').addClass('hide');
		$('.big'+curr).removeClass('hide');
	})
	// 评分效果
    $("ul.rating li a").click(function(){
	     var title = $(this).attr("title");
	     //alert("您给此商品的评分是："+title);
		 var cl = $(this).parent().attr("class");
		 $(this).parent().parent().removeClass().addClass("rating "+cl+"star");
		 $('.rating').val($(this).html());
		 $(this).blur();//去掉超链接的虚线框
		 return false;
	})
	// 新闻/公告切换
	$('.tab_news li').hover(function(){
		$(this).siblings().removeClass('h');
		$('.'+$(this).siblings().attr('link')).addClass('hide');
		$(this).addClass('h');
		$('.'+$(this).attr('link')).removeClass('hide');
	})
	// 弹出 message
	if($.cookie('message')) { var s=$.cookie('message');s=s.replace(/\+/g,' ');$.notice(s);$.cookie('message',null); }
	// 弹出 warning
	if($.cookie('warning')) { var s=$.cookie('warning');s=s.replace(/\+/g,' ');$.warning(s);$.cookie('warning',null); };
 });
